package org.lzy.shop.viewmodel

import LoginRepositoryImpl
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.lzy.shop.base.BaseViewModel
import org.lzy.shop.response.LoginResponse
import org.lzy.shop.response.PicSmsResponse
import org.lzy.shop.viewmodel.repository.LoginRepository

/**
 * 登录的VM层
 */
class LoginViewModel :BaseViewModel() {

    private val loginRepository: LoginRepository = LoginRepositoryImpl()

    val loginLiveData = MutableLiveData<LoginResponse?>()
    val picSmsLiveData = MutableLiveData<PicSmsResponse?>()
    val smsLiveData = MutableLiveData<String?>()
    val registerLiveData = MutableLiveData<String?>()



    /**
     * 密码登录
     */
    fun login(phone: String, password: String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val result = loginRepository.login(phone, password)

                if (result != null) {
                    loginLiveData.value = result
                } else {
                    errorMessage.value = "登录失败"
                    loginLiveData.value = null
                }
            } catch (e: Exception) {
                errorMessage.value = e.message ?: "登录失败"
                loginLiveData.value = null
            }
        }
    }

    /**
     * 短信验证码登录
     */
    fun loginSms(phone: String, smsCode: String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val result = loginRepository.loginSms(phone, smsCode)
                if (result != null) {
                    loginLiveData.value = result
                } else {
                    errorMessage.value = "登录失败"
                    loginLiveData.value = null
                }
            } catch (e: Exception) {
                errorMessage.value = e.message ?: "登录失败"
                loginLiveData.value = null
            }
        }
    }

    /**
     * 获取图片验证码
     */
    fun getPicSms() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val result = loginRepository.getPicSms()

                if (result != null) {
                    picSmsLiveData.value = result
                } else {
                    errorMessage.value = "获取验证码失败"
                    picSmsLiveData.value = null
                }
            } catch (e: Exception) {
                errorMessage.value = e.message ?: "获取验证码失败"
                picSmsLiveData.value = null
            }
        }
    }

    /**
     * 发送短信验证码
     */
    fun sendSms(phone: String, captchaId: String, code: String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val result = loginRepository.sendSms(phone, captchaId, code)

                if (result != null) {
                    smsLiveData.value = result
                } else {
                    errorMessage.value = "发送验证码失败"
                    smsLiveData.value = null
                }
            } catch (e: Exception) {
                errorMessage.value = e.message ?: "发送验证码失败"
                smsLiveData.value = null
            }
        }
    }

    /**
     * 注册
     */
    fun register(phone: String, smsCode: String, password: String, confirmPassword: String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val result = loginRepository.register(phone, smsCode, password, confirmPassword)

                if (result != null) {
                    registerLiveData.value = result
                } else {
                    errorMessage.value = "注册失败"
                    registerLiveData.value = null
                }
            } catch (e: Exception) {
                errorMessage.value = e.message ?: "注册失败"
                registerLiveData.value = null
            }
        }
    }

    /**
     * QQ授权登录
     */
    fun loginQQ(accessToken: String, openId: String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val result = loginRepository.loginQQ(accessToken, openId)

                if (result != null) {
                    loginLiveData.value = result
                } else {
                    errorMessage.value = "QQ登录失败"
                    loginLiveData.value = null
                }
            } catch (e: Exception) {
                errorMessage.value = e.message ?: "QQ登录失败"
                loginLiveData.value = null
            }
        }
    }

}